package com.menglei.dailytasks;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.menglei.dailytasks.service.ServiceBind;
import com.menglei.dailytasks.service.ServiceDemo;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 7/14 create service
     */
    private static final String TAG = "MainActivity:";
    private Button mStartService;
    private Button mStopService;
    private Intent intent;
    private ServiceConnection conn;
    private Intent intent2;
    private ServiceBind.MyBinder myBinder;
    /**
     * Stop!
     */
    private Button mDestroyService;
    /**
     * Bind!
     */
    private Button mBindService;
    /**
     * play!
     */
    private Button mPlayMusic;
    /**
     * pause!
     */
    private Button mPauseMusic;
    /**
     * UnBind!
     */
    private Button mUnBindService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        mStartService = (Button) findViewById(R.id.startService);
        mStartService.setOnClickListener(this);
        mStopService = (Button) findViewById(R.id.destroyService);
        mStopService.setOnClickListener(this);
        intent = new Intent(this, ServiceDemo.class);
        intent2 = new Intent(this, ServiceBind.class);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "MainActivity onServiceDisconnected");
                myBinder = (ServiceBind.MyBinder) service;

                //myBinder.onPause();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "MainActivity onServiceDisconnected");

            }
        };
        mDestroyService = (Button) findViewById(R.id.destroyService);
        mBindService = (Button) findViewById(R.id.bindService);
        mBindService.setOnClickListener(this);
        mPlayMusic = (Button) findViewById(R.id.playMusic);
        mPlayMusic.setOnClickListener(this);
        mPauseMusic = (Button) findViewById(R.id.pauseMusic);
        mPauseMusic.setOnClickListener(this);
        mDestroyService.setOnClickListener(this);
        mUnBindService = (Button) findViewById(R.id.unBindService);
        mUnBindService.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.startService:
                Log.d(TAG, "MainActivity btn_start_service onClick");
                v.getContext().startService(intent);
                break;
            case R.id.destroyService:
                Log.d(TAG, "MainActivity btn_stop_service onClick");
                v.getContext().stopService(intent);
                break;
            case R.id.bindService:
                v.getContext().bindService(intent2, conn, Context.BIND_AUTO_CREATE);
                break;
            case R.id.playMusic:
                if (myBinder==null){
                    Toast.makeText(this, "请先绑定服务", Toast.LENGTH_SHORT).show();
                    break;
                }
                myBinder.onPlay();
                break;
            case R.id.pauseMusic:
                myBinder.onPause();
                break;
            case R.id.unBindService:
                v.getContext().unbindService(conn);
                break;
        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}